package com.psq.core.common.utils

import okhttp3.internal.and
import java.security.MessageDigest

fun String.MD5(): String {
    return encrypt("MD5")
}

fun String.SHA1(): String {
    return encrypt("SHA-1")
}

private fun String.encrypt(algorithm: String): String {
    val instance = MessageDigest.getInstance(algorithm)
    instance.update(toByteArray())
    val digest = instance.digest()
    var crypt = ""
    for (i in digest.indices) {
        val hexString = Integer.toHexString(digest[i] and 0xFF)
        if (hexString.length == 1) {
            crypt += "0"
        }
        crypt += hexString
    }
    return crypt
}

